import 'dart:convert';

main(List<String> args) {
  var job = new Job("Software Developer", 7500, "Julia ComputingLLC");
  var jsonStr = job.toJson();
  print(jsonStr);
  var job2 = new Job.fromJson(jsonStr);
  assert(job2 is Job);
  assert(job2.toJson() == jsonStr);

  print( job2.toJson() );
  // 等价的
  assert( json.encode(job) == json.encode(job.toJson()) );
}


class Job {
  String type;
  int salary;
  String company;

  Job(this.type, this.salary, this.company);

  Map<String, Object> toMap() {
    var jsm = new Map<String, Object>();
    jsm["type"] = type;
    jsm["salary"] = salary;
    jsm["company"] = company;

    return jsm;
  }

  String toJson() {
    print("执行 Job.toJson() ...");

    var jss = json.encode( this.toMap() );
    return jss;
  }

  Job.fromJson(String jsonStr) {
    Map jsm = json.decode(jsonStr);
    this.type = jsm["type"];
    this.salary = jsm["salary"];
    this.company = jsm["company"];
  }
}
